package com.ef.web.action;

import java.text.SimpleDateFormat;
import java.util.List;

import com.ef.constant.EClassEntityType;
import com.ef.constant.EClassType;
import com.ef.constant.SystemContant;
import com.ef.domain.ClassEntity;
import com.ef.domain.ClassFocus;
import com.ef.domain.Classroom;
import com.ef.domain.Leads;
import com.ef.domain.LeadsClassEntity;

/**
 * 订课时的Class Entity.<br/>
 * 
 * @author CYG create:2009-12-12 下午06:26:56
 */
public class BookingClassEntity {

	/**
	 * 排课的class Entity;
	 */
	private ClassEntity classentity;

	/**
	 * 已选择的leadsClass
	 */
	private List<LeadsClassEntity> selectedLeads;

	private Boolean isF2FFree = false;

	private Boolean isF2FChinese = false;

	private Boolean isF2FFull = false;

	private Boolean isF2FAll = false;

	private Boolean isF2FMatch = true;

	private Boolean isFull = false;

	/**
	 * 是否已选择
	 */
	private Boolean isSelected = false;

	/**
	 * 是否已上课
	 */
	private Boolean isCheckin = false;

	/**
	 * 是否已缺席
	 */
	private Boolean isAbsence = false;

	/**
	 * 是否已等待
	 */
	private Boolean isWaiting = false;

	/**
	 * 是否已取消
	 */
	private Boolean isCancel = false;

	public BookingClassEntity() {

	}

	/**
	 * 该学生以及选择过的。
	 * 
	 * @param leadsClassEntity
	 */
	public BookingClassEntity(LeadsClassEntity leadsClassEntity,
			List<LeadsClassEntity> selectedLeads) {
		classentity = leadsClassEntity.getClassEntity();
		this.selectedLeads = selectedLeads;
		this.isCheckin = leadsClassEntity.isCheckin();
		this.isAbsence = leadsClassEntity.isAbsence();
		this.isSelected = leadsClassEntity.isBooking();
		this.isWaiting = leadsClassEntity.isWaiting();
		this.isCancel = leadsClassEntity.isCancel();
		preprareClassEntity();
	}

	private void preprareClassEntity() {
		if (classentity == null) {
			return;
		}
		if (classentity.isF2F()) {
			if (classentity.getBookingCount() == 0) {
				if (!classentity.getTeacher().isForeign()) {
					isF2FChinese = true;
				} else {
					isF2FAll = true;
				}
			}
			if (classentity.isFull()) {
				isF2FFull = true;
			}

		}
		if (classentity.isFull()) {
			isFull = true;
		}
 
	}

	/**
	 * 未被该学生选择过的。
	 * 
	 * @param classEntity
	 */
	public BookingClassEntity(ClassEntity classEntity,
			List<LeadsClassEntity> selectedLeads, Leads leads) {
		this.classentity = classEntity;
		this.selectedLeads = selectedLeads;
		if (classentity.isF2F()) {
			for (LeadsClassEntity lEntity : selectedLeads) {
				if (lEntity.getLeads().getCurrentlevel() == null
						|| leads.getCurrentlevel() == null) {
					isF2FMatch = false;
					break;
				}
				if (lEntity.isBooking() || lEntity.isWaiting()) {
					if (!lEntity.getLeads().getCurrentlevel().equals(
							leads.getCurrentlevel())) {
						isF2FMatch = false;
						break;
					}
				}
			}
		}
		preprareClassEntity();
	}

	private String message;

	public String getMessage() {
		if (classentity == null) {
			return "";
		} else {
			int count = classentity.getAllStudent();
			ClassFocus focus = classentity.getClassFocus();
			Classroom room = classentity.getClassroom();
			// String ef = "";
			// if (classentity.getType().equals(EClassEntityType.PSA)
			// || classentity.getType().equals(EClassEntityType.VIP)) {
			// ef = classentity.getType().toString();
			// }
			// message = count + " " + focus.getName() + "@"
			// + classentity.getClassType() + "  " + room.getName()
			// + " "
			// + ef;
			String fname = "";
			String rname = "";
			if (focus != null) {
				fname = focus.getName();
			}
			if (room != null) {
				rname = room.getName();
			}
			if (classentity.getType().equals(EClassEntityType.PSA)) {
				message = count + " " + fname + "[PSA]@" + rname;
				return message;
			}
			if (classentity.getType().equals(EClassEntityType.VIP)) {
				message = count + " " + fname + "[VIP]@" + rname;
				return message;
			}
			message = count + " " + fname + "@" + rname;
			return message;
		}
	}

	/**
	 * 获取当前选课情况的状态.<br/>
	 * 
	 * @return 返回显示颜色参数
	 */
	public String getStatusColor() {
		// @NOTE 注意以下判断顺序不能变化。
		if (isCheckin) {
			return SystemContant.BOOKING_CHECKIN_COLOR;
		}
		if (isAbsence) {
			return SystemContant.BOOKING_ABSENCE_COLOR;
		}
		if (isCancel) {
			return SystemContant.BOOKING_PUNISH_COLOR;
		}
		if (isWaiting) {
			return SystemContant.BOOKING_WAIT_COLOR;
		}

		if (isSelected) {
			return SystemContant.BOOKING_SELECTED_COLOR;
		}

		if (classentity.isF2F()) {
			if (isF2FFull) {
				return SystemContant.BOOKING_F2FFULL_COLOR;
			}
			if (isF2FChinese) {
				return SystemContant.BOOKING_F2FCHINESE_COLOR;
			}
			if (isF2FAll) {
				return SystemContant.BOOKING_F2FALL_COLOR;
			}
			if (!isF2FMatch) {
				return SystemContant.BOOKING_F2FUNMATCH_COLOR;
			}
			if (isF2FFree) {
				return SystemContant.BOOKING_F2FFREE_COLOR;
			}

			if (isF2FMatch) {
				return SystemContant.BOOKING_F2FMATCH_COLOR;
			}

		}

		if (isFull) {
			return SystemContant.BOOKING_FULL_COLOR;
		}

		return SystemContant.BOOKING_NORMAL;
	}

	static protected SimpleDateFormat dateformat = new SimpleDateFormat(
			"MM-dd HH:mm");

	public String getToolTip() {
		StringBuffer tooltip = new StringBuffer();
		for (LeadsClassEntity ce : selectedLeads) {
			Leads leads = ce.getLeads();
			tooltip.append(leads.getName()).append(" at ");
			if (ce.getBookingTime() != null) {
				tooltip.append(dateformat.format(ce.getBookingTime())).append(
						" ");
				if (ce.getClassOrder() != null) {
					tooltip.append("ORDER:").append(ce.getClassOrder()).append(
							" ");
				}
				;
				tooltip.append(ce.getClassStatus());
				if (ce.getComments() != null
						&& ce.getComments().trim().length() > 0) {
					tooltip.append(" ").append(ce.getComments().trim()).append(
							" ");
				}
				tooltip.append("\n");
			}

		}
		return tooltip.toString();
	}

	public ClassEntity getClassentity() {
		return classentity;
	}

	public void setClassentity(ClassEntity classentity) {
		this.classentity = classentity;
	}
}
